<?php

namespace App\Model;


use App\Common\Log;
use App\Common\SysContext;
use App\Common\User\WxUser;
use App\ModelBase\__t_wx_user;
use XWX\Common\H;

class t_wx_user extends __t_wx_user
{
    /**
     * 获取用户实体
     *
     * @param $openid
     * @param bool $get_redis true:先找redis;false:不找redis;
     * @return t_wx_user|null
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    static function funcGetWxuser($openid, $get_redis = true)
    {
        Log::funcLog(__METHOD__, 'mysql找用户');
        $data = SysContext::funcInsStatic()->db()
            ->where('wxuser_x_id', SysContext::funcInsStatic()->funcGetX())
            ->where('wxuser_openid', $openid)
            ->getOne(t_wx_user::table());
        if ($data != null)
        {
            Log::funcLog(__METHOD__, 'mysql找到用户');
        }

        if ($data === null && $get_redis)
        {
            Log::funcLog(__METHOD__, 'redis找用户');

            $key = WxUser::redisKeyUser($openid);
            $string1 = SysContext::funcInsStatic()->redis()->get($key);
            if (H::funcStrHasAnyText($string1))
            {
                Log::funcLog(__METHOD__, 'redis找到用户');

                $data = json_decode($string1, true);
            }
        }

        $entity = null;
        if ($data !== null)
        {
            $entity = t_wx_user::funcArrayToEntity($data);
        }


        return $entity;
    }
}